//1. 公用样式
require("../../assets/css/index.less");
require("../../assets/css/animate.min.css");
require("../../assets/fonts/iconfont.css");
require("../../lib/swiper/swiper-bundle.css");
//2. 当前页面样式
require("./home.less");

//3. 公用js
let dom = require("../../utils/dom.js");
let Swiper = require("../../lib/swiper/swiper-bundle.js");
let local = require("../../utils/local.js");
let https = require("../../utils/https.js");
//4. 当前页面的js
// console.log(dom);

dom.ready(function () {
  dom.isLogin();
  dom.bMenu("home");
  new Swiper(".swiper-container", {
    loop: true, // 允许图片循环
    pagination: {
      // 显示轮播分页
      el: ".swiper-pagination",
    },
    autoplay: true, // 自动播放
  });

  function getUserInfo() {
    https.get("/api/user/info", {}, function (res) {
      if (res.errno == 0) {
        local.set("userData", res.data);
        let { badges, clockCount, ranking } = res.data;
        dom.getEle("#rankCount").innerHTML = ranking;
        dom.getEle("#punchSum").innerHTML = clockCount;
        dom.getEle("#badgeCount").innerHTML = badges;
      }
    });
  }
  getUserInfo();

  function getClockInfo() {
    https.get("/api/user/clockInInfo", {}, function (res) {
      console.log(res);
      if (res.errno == 0) {
        checkBtn.innerHTML = res.data.isClockIn ? "已经打卡" : "今日打卡";
      }
    });
  }
  getClockInfo();

  dom.getEle("#checkBtn").addEventListener("click", function () {
    https.post("/api/user/clockIn", {}, function (res) {
      getClockInfo();
      if (res.errno == -1) {
        alert(res.message);
      } else {
        alert("打卡成功");
      }
    });
  });
});
